package cn.liumouren.jzxd.service.impl;

import cn.liumouren.jzxd.common.utils.PageUtils;
import cn.liumouren.jzxd.common.utils.Query;
import cn.liumouren.jzxd.dao.LikesDao;
import cn.liumouren.jzxd.dao.UserDao;
import cn.liumouren.jzxd.entity.Likes;
import cn.liumouren.jzxd.service.LikesService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("likesService")
public class LikesServiceImpl extends ServiceImpl<LikesDao, Likes> implements LikesService {

    @Resource
    private LikesDao likesDao;
    @Resource
    private UserDao userDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<Likes> page = this.page(
                new Query<Likes>().getPage(params),
                new QueryWrapper<Likes>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<Likes> getAllLikes(String zoneId) {
        /*List<Likes> likesList = baseMapper.selectList(new QueryWrapper<Likes>().lambda()
                .eq(Likes::getZoneId, zoneId)
                .orderByDesc(Likes::getAddTime));
        mapperUtils.getUserByEntity(likesList, userDao);
        return likesList;*/
        return likesDao.getAllLikesByZoneId(zoneId);
    }

    @Override
    public Likes findByUserIdAndZoneId(String zoneId, String userId) {
        return super.getOne(new QueryWrapper<Likes>().lambda()
                .eq(Likes::getZoneId, zoneId)
                .eq(Likes::getUserId, userId));
    }

    @Override
    public void notLikeNoMore(String zoneId, String userId) {
        baseMapper.delete(new QueryWrapper<Likes>().lambda()
                .eq(Likes::getZoneId, zoneId)
                .eq(Likes::getUserId, userId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(Likes entity) {
        entity.setId(IdWorker.getIdStr());
        entity.setAddTime(new Date());
        return SqlHelper.retBool(baseMapper.insert(entity));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(Likes entity) {
        return SqlHelper.retBool(baseMapper.updateById(entity));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return false;
        }
        return SqlHelper.retBool(baseMapper.deleteBatchIds(idList));
    }

}